package sk.stuba.fiit.repka.dp.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;

public class RecommendationsDialog extends JDialog implements ActionListener{

	private final JPanel contentPanel = new JPanel();
	private List<String> recommendations;
	private JPanel panelWithCheckBoxes;
	private List<JCheckBox> checkBoxes = new ArrayList<JCheckBox>();
	private List<String> selectedRecommendations;
	
	public List<String> getSelectedRecommendations() {
		if(this.selectedRecommendations == null) {
			this.selectedRecommendations = new ArrayList<String>();
		}
		return this.selectedRecommendations;
	}

	/**
	 * Create the dialog.
	 */
	public RecommendationsDialog(List<String> recommedations) {
		this.recommendations = new ArrayList<String>(recommedations);
		this.setModal(true);
		this.setTitle("RECOMMENDATIONS");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new GridLayout(1, 0, 0, 0));

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		add(scrollPane);
		
		if(this.recommendations.size() == 0) {
			scrollPane.setViewportView(this.initialNotAvailablePanel());
		} else {
			panelWithCheckBoxes = new JPanel();
			panelWithCheckBoxes.setLayout(new GridLayout(0, 2));
			scrollPane.setViewportView(panelWithCheckBoxes);
			this.generateCheckBox(this.recommendations);
		}

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}

		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	
	private JPanel initialNotAvailablePanel() {
		JPanel recoNotAvailable = new JPanel();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		recoNotAvailable.setLayout(gridBagLayout);
		
		JLabel lblNoRecommendationsAvailable = new JLabel("No RECOMMENDATIONS available!");
		lblNoRecommendationsAvailable.setFont(new Font("Tahoma", Font.BOLD, 16));
		GridBagConstraints gbc_lblNoRecommendationsAvailable = new GridBagConstraints();
		gbc_lblNoRecommendationsAvailable.gridx = 0;
		gbc_lblNoRecommendationsAvailable.gridy = 0;
		recoNotAvailable.add(lblNoRecommendationsAvailable, gbc_lblNoRecommendationsAvailable);
		return recoNotAvailable;
	}

	// generate checkboxes from recommendations
	private void generateCheckBox(List<String> inList) {
		Iterator<String> iter = inList.iterator();
		while (iter.hasNext()) {
			String s = iter.next();
			JCheckBox chckbxNewCheckBox = new JCheckBox(s);
			this.checkBoxes.add(chckbxNewCheckBox); // add checkbox to list
			this.panelWithCheckBoxes.add(chckbxNewCheckBox);
		}
	}

	private void setSelectedRecommendations() {
		for (JCheckBox c : this.checkBoxes) {
			if (c.isSelected()) {
				this.getSelectedRecommendations().add(c.getText());
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if("OK".equals(e.getActionCommand())) {
			this.setSelectedRecommendations();
			this.dispose();
		} else if("Cancel".equals(e.getActionCommand())) {
			this.dispose();
		}
	}
}
